package com.mock.ems.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.StringUtil;
import com.mock.ems.config.UserManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 资金明细
 * @author whx
 * @date 2017/08/19
 */
public class FundsRecordController extends Controller {

    public void index()  {
        List<Record> typeList = Db.find("select type_id,name from module_type where module = ? ","user_funds");
        setAttr("typeList",typeList);
        renderJsp("/fundsrecord_list.jsp");
    }

    public void pagination() {
        long start = System.currentTimeMillis();
        int limit = getParaToInt("limit");
        int offset = getParaToInt("offset");
        int pageNumber = (offset / limit) + 1;

        String selectSql = "select * ";
        StringBuilder sqlExceptSql = new StringBuilder(" from user_funds_record where 1 = 1");
        List<Object> params = new ArrayList<Object>();

        String nickName = getPara("nickName");
        if (StringUtil.isNotEmpty(nickName)) {
            List<Record> userRecordList = Db.find("select id from user where nick_name like ? ",'%' + nickName.trim() + '%');
            if (CollectionUtil.isNotEmpty(userRecordList)) {
                sqlExceptSql.append(" and user_id in ( ");
                userRecordList.stream().forEach(item -> {
                    sqlExceptSql.append("?,");
                    params.add(item.getLong("id"));
                });
                sqlExceptSql.deleteCharAt(sqlExceptSql.length() - 1);
                sqlExceptSql.append(") ");
            }
        }

        String orderNo = getPara("order_no", "");
        if (StringUtil.isNotEmpty(orderNo.trim())) {
            sqlExceptSql.append( " and order_no = ?");
            params.add(orderNo.trim());
        }

        int type = getParaToInt("type", 0);
        if (0 != type) {
            sqlExceptSql.append( " and type = ?");
            params.add(type);
        }
        sqlExceptSql.append(" order by id desc ");
        Page<Record> recordList = Db.paginate(pageNumber, limit, selectSql, sqlExceptSql.toString(), params.toArray());
        if(CollectionUtil.isNotEmpty(recordList.getList())){

            List<Record> typeList = Db.find("select type_id,name from module_type where module = ? ","user_funds");
            Map<Integer,String> typeMap = new HashMap<Integer, String>();

            typeList.stream().forEach(item->{
                typeMap.put((Integer)item.get("type_id") ,item.get("name").toString());
            });

            recordList.getList().stream().forEach(item->{
                Long userId = item.getLong("user_id");
                Record user = UserManager.getUser(userId);
                if(user == null){
                    user = Db.findById("user",userId);
                    if(user != null){
                        UserManager.setUser(user);
                    }
                }
                item.set("nick_name",user == null ? "未知" : user.getStr("nick_name"));
                item.set("type",typeMap.get(item.getInt("type")));
            });
        }
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());

        renderJson(returnMap);
    }
}
